<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="6">
            <a-form-item label="客户">
              <j-search-select-tag v-model="queryParam.custom" dict="hf_supplier,name,name" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="销售订单号">
              <a-input type="list" v-model="queryParam.saleOrder" dictCode="hf_input_status" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="供应商订单号">
              <a-input v-model="queryParam.customOrderSerial" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="6" :sm="6">
            <a-form-item label="关闭标志">
              <j-search-select-tag
                v-model="queryParam.closed"
                :dictOptions="[
                  { text: '关闭', value: '1' },
                  { text: '未关闭', value: '0' }
                ]"
                @change="searchQuery"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :md="6" :sm="6">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a-button @click="handleAdd" type="primary" icon="plus" style="margin-left: 8px">新增</a-button>
              <j-super-query
                :field-list="superQueryFieldList"
                ref="superQueryModal"
                @handleSuperQuery="handleSuperQuery"
                style="margin-left: 8px"
              ></j-super-query>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :customRow="clickThenSelect"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a v-if="record.closed === '1'" @click="closeOrder(record.id, '0')">反关闭</a>
          <a-popconfirm title="确定关闭吗?" @confirm="() => closeOrder(record.id, '1')">
            <a v-if="record.closed === '0'">关闭</a>
          </a-popconfirm>
        </span>
        <span slot="process" slot-scope="text, record">
          <a-progress :percent="record.process" size="small" />
        </span>
        <span slot="tags" slot-scope="text, record">
          <a-tag color="red" v-if="record.closed === '0'">未关闭</a-tag>
          <a-tag color="green" v-if="record.closed === '1'">关闭</a-tag>
        </span>
      </a-table>
    </div>

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="采购订单执行明细" key="1">
        <HfSaleOrderConditionDetail
          :mainId="selectedMainId"
          ref="HfPurchaseOrderConditionDetail"
        ></HfSaleOrderConditionDetail>
      </a-tab-pane>
    </a-tabs>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { jsonToForm } from '@/utils/util'
import { getAction, putAction } from '@api/manage'
import HfSaleOrderConditionDetail from '@views/sale/table/HfSaleOrderConditionDetail.vue'

export default {
  name: 'HfSaleOrderCondition',
  components: { HfSaleOrderConditionDetail },
  mixins: [JeecgListMixin],
  data() {
    return {
      description: '销售订单执行情况',
      // 表头
      columns: [
        {
          title: '销售订单号',
          align: 'center',
          dataIndex: 'saleOrder'
        },
        {
          title: '客户订单号',
          align: 'center',
          dataIndex: 'customOrderSerial'
        },
        {
          title: '订单状态',
          align: 'center',
          dataIndex: 'status'
        },
        {
          title: '客户',
          align: 'center',
          dataIndex: 'custom'
        },
        {
          title: '完成进度',
          align: 'center',
          dataIndex: 'process',
          scopedSlots: { customRender: 'process' }
        },
        {
          title: '关闭标志',
          align: 'center',
          scopedSlots: { customRender: 'tags' }
        },
        {
          title: '单据日期',
          align: 'center',
          dataIndex: 'documentDate',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
        {
          title: '结算日期',
          align: 'center',
          dataIndex: 'paymentTime',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
        {
          title: '制单',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '审核人',
          align: 'center',
          dataIndex: 'checker'
        },
        {
          title: '审核人',
          align: 'center',
          dataIndex: 'checkTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/sale/hfSaleOrder/conditionList',
        closeOrder: '/sale/hfSaleOrder/closeOrder'
      },
      dictOptions: {
        supplier: [],
        purchaseType: [],
        payType: []
      },
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '30', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      selectedMainId: '',
      superQueryFieldList: [],
      superQueryFlag: false,
      superQueryParams: ''
    }
  },
  created() {
    this.getSuperFieldList()
    this.loadData()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    initDictConfig() {},
    // 关闭订单
    closeOrder(orderId, closed) {
      // 定义参数对象
      let params = { orderId: orderId, closed: closed }
      // 将参数对象转换为表单数据
      params = jsonToForm(params)
      // 发起关闭订单的请求
      putAction(this.url.closeOrder, params).then(res => {
        // 如果关闭订单成功
        if (res.success) {
          // 显示成功提示
          this.$message.success(res.message)
          // 重新加载数据
          this.loadData()
        } else {
          // 显示警告提示
          this.$message.warn(res.message)
        }
      })
    },

    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.onClearSelected()
      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params).then(res => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'orderSerial', text: '订单编号' })
      fieldList.push({ type: 'string', value: 'status', text: '审核标志', dictCode: 'hf_input_status' })
      fieldList.push({ type: 'string', value: 'supplier', text: '供应商', dictCode: 'hf_supplier,name,id' })
      fieldList.push({ type: 'string', value: 'fromOrderSerial', text: '关联销售订单号' })
      fieldList.push({ type: 'sel_user', value: 'operator', text: '制单' })
      fieldList.push({ type: 'sel_user', value: 'checker', text: '审核人' })
      fieldList.push({ type: 'int', value: 'process', text: '完成进度' })
      fieldList.push({ type: 'date', value: 'deliveryTime', text: '交货日期' })
      fieldList.push({ type: 'date', value: 'documentDate', text: '单据日期' })
      this.superQueryFieldList = fieldList
    }
  }
}
</script>

<style scoped></style>
